﻿<%@LANGUAGE="JSCRIPT"%>
<%
    var configurationManager = Server.CreateObject("oxWebCATICom.CATIStaticProcedures");
    configurationManager.SetRootPath(Server.MapPath("/OxWebCATI"));
    configurationManager = null;

    var surveyName = new String(Request.Item("p"));
    Response.Buffer = false;
    Response.ContentType = "application/xml";
    
    var surveyDB = Server.CreateObject("ADODB.Connection");
    surveyDB.CursorLocation = 3; // adUseClient
    surveyDB.Open("Driver=Microsoft Access Driver (*.mdb);DBQ=" + Server.MapPath("/OxWebCATI/SurveyData/" + surveyName + "/surveyData.mdb"));

    var surveyRST = Server.CreateObject("ADODB.Recordset");
    // 3, 1, 1 --> adOpenStatic, adLockReadOnly, adCmdText
    surveyRST.Open("SELECT * FROM tblExtraData WHERE OPTIN = '1' AND OPTINSERVED = '0'", surveyDB, 3, 1, 1);

    Response.Write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n");
    Response.Write("    <optinlist>\r\n");

    if(surveyRST.RecordCount > 0)
    {
        for(var optinIndex = 0; optinIndex < surveyRST.RecordCount; optinIndex++)
        {
            Response.Write("        <optin ");
            for(var fieldIndex = 0; fieldIndex < surveyRST.Fields.Count - 1; fieldIndex++)
            {
                Response.Write(surveyRST.Fields.Item(fieldIndex).Name + "=\"");
                Response.Write(surveyRST(fieldIndex).Value);
                Response.Write("\" ");
            }
            Response.Write(" />");
            surveyRST.MoveNext();
        }
    }

    var lngRecordsAffected = 0;
    surveyDB.Execute("UPDATE tblExtraData SET OPTINSERVED = '1' WHERE OPTIN = '1' AND OPTINSERVED = '0'", lngRecordsAffected);

    surveyRST.Close();
    surveyDB.Close();
    surveyRST = null;
    surveyDB = null;

    Response.Write("    </optinlist>\r\n");
%>
     